package mth.masterimp;

import com.rapidminer.datatable.DataTableRow;
import com.rapidminer.datatable.SimpleDataTable;

public class Tools {
	/*
	 * In dieser Klasse werden fast ausschließlich statische Methoden zur Datenbearbeitung 
	 * zur Verfügung gestellt. Diese dienen als Helfer um häufig auftretende Probleme zu
	 * behandeln.
	 */
	
	public static int[] addIntegerToArray(int[] array, int val) {
		if (array==null || array.length==0) 
			array = new int[0];
		int[] helper = new int[array.length+1];
		for (int a=0; a<array.length; a++) {
			helper[a]=array[a];
		}
		helper[array.length] = val;
		array = new int[helper.length];
		array=helper;
		return array;
	}
	
	public static boolean doubleArrayContains(double[] array, double val) {
		if (array==null || array.length==0) 
			return false;
		for (int a=0; a<array.length; a++) {
			if (array[a]==val)
				return true;
		}
		return false;
	}
	
	/**
	 * This method is for extracting the values of a DataTableRow as double values.
	 * Because I was not able to get a working Cast for DataRow->double[]
	 * 
	 * There is no fault detection. If the values are not Double values, it will crash.
	 * Maybe I will fix it later. TODO
	 * 
	 * @param dataTableRow
	 * @return double[] essence alias the values of the DataTableRow
	 */
	public static double[] extractDoubleArrayFromDataRow(DataTableRow dataTableRow) {
		DataRepresentator dr = new DataRepresentator();
		double[] essence = new double[dataTableRow.getNumberOfValues()-dr.getValueMatrix().getNumberOfSpecialColumns()];
		for (int a=0; a<essence.length; a++) {
			essence[a]=dataTableRow.getValue(a);
		}
		return essence;
	}
	
	public static double[] extractLongDoubleArrayFromDataRow(DataTableRow dataTableRow) {
		double[] essence = new double[dataTableRow.getNumberOfValues()];
		for (int a=0; a<essence.length; a++) {
			essence[a]=dataTableRow.getValue(a);
		}
		return essence;
	}
	
	public double[][] extractDoubleMatrixFromSimpleDataTable(SimpleDataTable dataTable) {
		DataRepresentator dr = new DataRepresentator();
		double[][] data = new double[dataTable.getNumberOfRows()][dataTable.getNumberOfColumns()-dr.getValueMatrix().getNumberOfSpecialColumns()];
		double[] longData = new double[dataTable.getNumberOfColumns()];
		double[] shortData = new double[dr.getValueMatrix().getNumberOfColumns()-dr.getValueMatrix().getNumberOfSpecialColumns()];
		for (int a=0; a<dataTable.getNumberOfRows(); a++) {
			longData=extractDoubleArrayFromDataRow(dataTable.getRow(a));
			for (int b=0; b<shortData.length; b++) {
				shortData[b]=longData[b];
			}
			data[a]=shortData;
		}
		return data;
	}
	
	public static boolean intArrayContains(int[] array, int val) {
		if (array==null || array.length==0) 
			return false;
		for (int a=0; a<array.length; a++) {
			if (array[a]==val)
				return true;
		}
		return false;
	}

}
